/******************************************************************************************

Made by Flashphoner. All rights reserved.
These sources can only be used for working with Flashphoner server.
If you want use this to create your own application (separate from Flashphoner server) –
please contact support@flashphoner.com

******************************************************************************************/
package com.flashphoner.app;

import com.flashphoner.sdk.rtmp.IRtmpClient;
import com.flashphoner.sdk.rtmp.IRtmpClientsCollection;
import com.flashphoner.sdk.softphone.ISoftphone;
import com.flashphoner.sdk.softphone.SoftphoneCallState;
import com.flashphoner.sip.call.ISipCall;
import com.flashphoner.sip.call.SipCalls;
import com.wowza.wms.application.ApplicationInstance;
import com.wowza.wms.application.IApplicationInstance;
import com.wowza.wms.application.WMSProperties;
import com.wowza.wms.client.IClient;
import com.wowza.wms.http.IHTTPProvider;
import com.wowza.wms.http.IHTTPRequest;
import com.wowza.wms.http.IHTTPResponse;
import com.wowza.wms.logging.WMSLogger;
import com.wowza.wms.logging.WMSLoggerFactory;
import com.wowza.wms.vhost.HostPort;
import com.wowza.wms.vhost.IVHost;

/**
 * Created by IntelliJ IDEA.
 * User: Lexa
 * Date: 10.06.2009
 * Time: 18:43:52
 * To change this template use File | Settings | File Templates.
 */
public class HTTPStatisticFlashphoner implements IHTTPProvider {
    private WMSProperties props;
    private static WMSLogger log = WMSLoggerFactory.getLogger(HTTPStatisticFlashphoner.class);


    public void onBind(IVHost vhost, HostPort hostPort) {
    }

    public void onHTTPRequest(IVHost vhost, IHTTPRequest request, IHTTPResponse response) {

        try{
            log.info("Get statistic by HTTP");
            IApplicationInstance instance = FlashphonerApp.instance;
            if (instance == null){
                return;
            }
            IRtmpClientsCollection rtmpClientsCollection = RtmpClients.getInstance();
            int count = 0;
            int countTalk = 0;
            for (IClient client:instance.getClients()){
                IRtmpClient rtmpClient;
                try{
                    rtmpClient = rtmpClientsCollection.findByClient(client);
                }catch(NullPointerException ex){
                    continue;
                }
                if (rtmpClient == null) continue;
                ISoftphone softphone = rtmpClient.getSoftphone();
                SipCalls sipCalls = softphone.getCalls();
                for (String id: sipCalls.getCalls()){
                    ISipCall sipCall = sipCalls.getCall(id);
                    count++;
                    if (sipCall.getState() == SoftphoneCallState.TALK){
                        countTalk++;
                    }
                }
            }
            log.info("Statictis: total calls - "+count+"; calls-talk - "+countTalk);
            StringBuffer html = new StringBuffer();
            html.append("\r\n");
            html.append("\r\n");
            html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"\r\n");
            html.append("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\r\n");
            html.append("\r\n");
            html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"ru\" lang=\"ru\">\r\n");
            html.append("<head>\r\n");
            html.append("    <meta content=\"text/html; charset= charset=utf-8\" http-equiv=\"Content-Type\"/>\r\n");
            html.append("    <title>Statistics of calls</title>\r\n");
            html.append("    \r\n");
            html.append("\r\n");
            html.append("</head>\r\n");
            html.append("\r\n");
            html.append("<body>\r\n");
            html.append("<h2>count of calls - "+count+"</h2><br>");
            html.append("<h2>count of calls in state talk - "+countTalk+"</h2><br>");
            html.append("</body>\r\n");
            html.append("</html>\r\n");

            response.getOutputStream().write(html.toString().getBytes("UTF-8"));
            response.getOutputStream().flush();
            response.getOutputStream().close();
        } catch (Exception e) {
        }
    }

    public void onUnbind(IVHost ivHost, HostPort hostPort) {

    }

    public void setProperties(WMSProperties wmsProperties) {
        this.props = wmsProperties;
    }
}
